<template>
  <div>
    <a-card>
      <h2>权益开通工具</h2>
      <div class="table-page-search-wrapper" style="margin-top:24px">
        <a-form layout="inline" @keyup.enter.native="openRights">
          <a-row :gutter="48">
            <a-col :xxl="6" :lg="8" :md="12" :sm="24">
              <a-form-item label="权益包类型">
                <a-select v-model="queryParam.memberCardId" placeholder="请选择" :getPopupContainer="triggerNode => triggerNode.parentNode">
                  <!-- <a-select-option value="1161520807288602625">先试后买特权</a-select-option>
                  <a-select-option value="1161520807288602626">1V1搭配师服务特权</a-select-option> -->
                  <a-select-option :value="item.id" v-for="item in cardList" :key="item.id">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xxl="6" :lg="8" :md="12" :sm="24">
              <a-form-item label="手机号">
                <a-input v-model="queryParam.phone" allowClear />
              </a-form-item>
            </a-col>
            <a-col :xxl="12" :lg="8" :md="24" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'left', overflow: 'hidden' }">
                <a-button type="primary" @click="openRights">开通</a-button>
                <a-button style="margin-left: 18px" @click="handleImportXls">导入</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-divider />
      <div class="record">
        <h2>后台开通记录</h2>
        <a-range-picker
          :show-time="{
            hideDisabledOptions: true,
            defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')]
          }"
          format="YYYY-MM-DD HH:mm:ss"
          :placeholder="['开始时间', '结束时间']"
          v-model="timePicker"
          @keyup.enter.native="searchList"
          @change="onChange"
          @ok="searchList"
        />
      </div>
      <s-table ref="table" size="default" :rowKey="record => record.id" :columns="columns" :data="loadData" :pagination="paginationProps"></s-table>
    </a-card>
    <j-import-modal ref="importModal" :uploadParams="getUploadParams()" @ok="importOk" :templateUrl="templateUrl()" :fileSize="0.5"></j-import-modal>
  </div>
</template>
<script>
import { listMixin } from '@/views/box/util/listMixin'
import moment from 'moment'
import 'moment/locale/zh-cn'
import globalData from '@/assets/js/common'
import JImportModal from '@/components/qiniuUpload/JImportModal'
import { openSubList, importSubcribeList, subcribeByPhoneAndCardId, tryBeforeAfterBuyCardList } from '@/api/box/marketing'
const columns = [
  { title: '会员', dataIndex: 'userName', align: 'center' },
  { title: '手机号', dataIndex: 'phone', align: 'center' },
  { title: '权益包类型', dataIndex: 'cardName' },
  { title: '开通状态', dataIndex: 'status', customRender: text => (text == 1 ? '开通' : '未开通') },
  { title: '备注', dataIndex: 'remark' },
  { title: '操作时间', align: 'center', dataIndex: 'updateTime' },
  { title: '操作人', dataIndex: 'createBy', align: 'center' }
]
export default {
  mixins: [listMixin],
  components: { JImportModal },
  data() {
    return {
      columns,
      timePicker: null,
      cardList: []
    }
  },
  computed: {
    loadData() {
      return parameter => {
        return this.fetchData(parameter)
      }
    }
  },
  mounted() {
    this.getCardList()
  },
  methods: {
    moment,
    getCardList() {
      tryBeforeAfterBuyCardList().then(res => {
        if (res.code == 0) {
          this.cardList = res.data
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    fetchData(parameter) {
      let formValue = {}
      if (this.timePicker && this.timePicker.length > 0) {
        formValue = {
          endTime: this.timePicker[1].format('YYYY-MM-DD HH:mm:ss'),
          startTime: this.timePicker[0].format('YYYY-MM-DD HH:mm:ss')
        }
      } else {
        formValue = {}
      }
      let params = {
        page: parameter,
        requestData: formValue
      }
      try {
        return this.fetchList(params, openSubList).then(res => {
          if (res.code == 0) {
            return res
          } else {
            this.$message.warning(res.msg)
            return {
              data: [],
              page: {
                pageNo: 1,
                pageSize: 10,
                count: 0
              }
            }
          }
        })
      } catch (err) {
        console.log(err.response)
      }
    },
    openRights() {
      if (!this.queryParam.memberCardId) {
        this.$message.warning('请选择权益包类型！')
        return
      }
      if (!this.queryParam.phone) {
        this.$message.warning('请输入手机号！')
        return
      }
      let params = {
        requestData: this.queryParam
      }
      subcribeByPhoneAndCardId(params).then(res => {
        if (res.code == 0) {
          this.$message.success('开通成功')
          this.queryParam = {}
          this.$refs.table.refresh(true)
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    // 导入
    handleImportXls() {
      this.$refs.importModal.show()
    },
    getUploadParams() {
      return { ...globalData.uploadParams }
    },
    templateUrl() {
      return 'https://img.bzhz.jnbygroup.com/rightsTemp.xlsx'
    },
    importOk(result, fileList) {
      console.log(result, fileList)

      let params = {
        requestData: fileList[0]
      }
      importSubcribeList(params).then(res => {
        if (res.code == 0) {
          this.$message.success('上传成功，请稍后刷新列表！')
          this.$refs.importModal.close()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    onChange(date, dateString) {
      if (date.length == 0) {
        this.searchList()
      }
    },
    handleOk(date) {
      this.searchList()
    }
  }
}
</script>
<style lang="less" scoped>
.record {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}
</style>

